package com.timeDeal.service;



import com.alibaba.druid.util.StringUtils;

import com.timeDeal.bean.ShijianchushouBean;
import com.timeDeal.bean.UserBean;
import com.timeDeal.bean.XuqiufabuBean;
import com.timeDeal.util.DButil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 叼爸爸 on 2016/9/4.
 */
public class AdminService {

    public boolean tijiaoGerenXinxi(UserBean ub) {
        Connection cn = DButil.getConnection();
        String mysql = "update user set zhenshixingming=? ,shouji=?,xingbie=?,nianling=?" +
                ",youxiang=?,tongxundizhi=?,shenfenzhenghao=?,gerenxintiao=?,shengri=?"
                + "where zhanghao=?";
        String mysql3 = "SELECT yunxuid  FROM  user WHERE zhanghao=?";
        String mysql2 = "update yunxu   set zhenshixingming=? ,shouji=?,xingbie=?,nianling=?" +
                ",youxiang=?,tongxundizhi=?,shenfenzhenghao=?,gerenxintiao=?,shengri=? " +
                "where  yunxuid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            int id = 0;
            ps = cn.prepareStatement(mysql3);
            ps.setString(1, ub.getZhanghao());
            rs = ps.executeQuery();
            if (rs.next()) {
                id = rs.getInt("yunxuid");
            }
            ps.clearParameters();
            ps = cn.prepareStatement(mysql);
            ps.setString(1, ub.getZhenshixingming());
            ps.setString(2, ub.getShouji());
            ps.setString(3, ub.getXingbie());
            ps.setInt(4, ub.getNianling());
            ps.setString(5, ub.getYouxiang());
            ps.setString(6, ub.getTongxundizhi());
            ps.setString(7, ub.getShenfenzhenghao());
            ps.setString(8, ub.getGerenxintiao());
            ps.setDate(9, ub.getShengri());
            ps.setString(10, ub.getZhanghao());
            boolean re = ps.execute();
            System.out.println(re);
            ps.clearParameters();
            ps = cn.prepareStatement(mysql2);
            for (int i = 1; i < 10; i++) {
                ps.setString(i, ub.getGongkai()[i]);
            }
            ps.setInt(10, id);
            re = ps.execute();
            System.out.println(re);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 管理员登录验证
     *
     * @param ub 账号
     * @param s  密码
     * @return 是否匹配
     */
    public boolean dologin(String ub, String s) {
        Connection cn = DButil.getConnection();
        String mysql = "select  * from  guanliyuan where zhanghao=? and mima=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = cn.prepareStatement(mysql);
            ps.setString(1, ub);
            ps.setString(2, s);
            rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt(1)>0 ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 管理员审核通过业务
     *
     * @param id 业务id
     * @param leixing   业务类型
     * @return boolean
     */
    public boolean shenHeTongGuo(int id, String leixing) {
        Connection conn = DButil.getConnection();
        String mysql = "update xuqiufabu set zhuangtai=? where xuqiufabuid=?";
        String mysq2 = "update shijianchushou set zhuangtai=? where shijianchushouid=?";
        PreparedStatement ps = null;
        boolean re =false ;
        try {
            if(StringUtils.equals(leixing,"xuqiufabu")) {
                ps = conn.prepareStatement(mysql);
                ps.setString(1, "审核通过");
                ps.setInt(2, id);
                 re = ps.execute();
            }
            else{
                ps = conn.prepareStatement(mysq2);
                ps.setString(1, "审核通过");
                ps.setInt(2, id);
                re = ps.execute();
            }
            return re;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 删除审核不通过业务
     *
     * @param id 业务id
     * @param leixing   业务类型
     * @return boolean
     */
    public boolean Delete(int id, String leixing) {
        Connection conn = DButil.getConnection();
        String mysql = "update xuqiufabu set zhuangtai=? where xuqiufabuid=?";
        String mysq2 = "update  shijianchushou set zhuangtai=?  where shijianchushouid=?";
        PreparedStatement ps = null;
        try {
            if(StringUtils.equals(leixing,"xuqiufabu")) {
                ps = conn.prepareStatement(mysql);
                ps.setString(1, "审核不通过");
                ps.setInt(2, id);
                ps.execute();
            }
            else{
                ps = conn.prepareStatement(mysq2);
                ps.setString(1, "审核不通过");
                ps.setInt(2, id);
                ps.execute();
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public List<Object> getAllByZhuangtai(String zhuangtai, int cu) {
        Connection cn = DButil.getConnection();
        List<Object> li = new ArrayList<>();
        String mysql = "select  *   from  xuqiufabu   where  zhuangtai=?  limit  " + (cu * 3) + "," + 3;
        String mysql2 = "select  *   from  shijianchushou   where  zhuangtai=?  limit  " + (cu * 3) + "," + 3;
        PreparedStatement ps = null;
        ResultSet rs = null;
        UserService userService = new UserService();
        try {
            ps = cn.prepareStatement(mysql);
            ps.setString(1, zhuangtai);
            rs = ps.executeQuery();
            int flag = 0;
            while (rs.next()) {
                XuqiufabuBean xb = new XuqiufabuBean();
                xb.setXuqiumingcheng(rs.getString("mingcheng"));
                xb.setMiaoshu(rs.getString("miaoshu"));
                xb.setJiafangid(rs.getInt("jiafangid"));
                xb.setTijiaorenXingming(userService.getDetailById(rs.getInt("xuqiufabuid")));
                li.add(xb);
                flag = li.size();
            }
            if (li.size() < 3) {
                ps.clearParameters();
                ps = cn.prepareStatement(mysql2);
                ps.setString(1, zhuangtai);
                rs = ps.executeQuery();
                while (rs.next()) {
                    ShijianchushouBean xb = new ShijianchushouBean();
                    xb.setShijianchushoumingcheng(rs.getString("mingcheng"));
                    xb.setBeizhu(rs.getString("beizhu"));
                    xb.setJiafangid(rs.getInt("jiafangid"));
                    xb.setTijiaorenXingming(userService.getDetailById(rs.getInt("xuqiufabuid")));
                    li.add(xb);
                }
            }
            li.add(flag);
            return li;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public   String  getCountByZhuangtai(String zhuangtai){
        Connection cn = DButil.getConnection();
        String mysql="select count( *)   from  xuqiufabu   where zhuangtai=?  "   ;
        String mysql2="select count( *)   from  shijianchushou   where zhuangtai=?  "  ;
        String  count="" ;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setString(1,zhuangtai);
            rs = ps.executeQuery();
            while (rs.next()){
                count =  rs.getString(1);
            }
            ps.clearParameters();
            ps=cn.prepareStatement(mysql2);
            ps.setString(1,zhuangtai);
            rs = ps.executeQuery();
            while (rs.next()){
                count = String.valueOf(Integer.valueOf(rs.getString(1))+Integer.valueOf(count));
            }
            System.out.println(count);
            return  count;
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  "" ;
    }
	
	 /**
     * 用户信用等级和警告次数的变更
     */
    public boolean jubaochulijieguo(int yonghuid,String chengfaleixing) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        String sql = "";
        if(chengfaleixing.equals("0")) {
            sql = "update user set xinyongdengji=ifnull(xinyongdengji,1)-1 where yonghuid=?";
        } else if(chengfaleixing.equals("1")) {
            sql = "update user set jinggaocishu=ifnull(jinggaocishu,0)+1 where yonghuid=?";
        }
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,yonghuid);
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

}
